#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;

int T, n, m, k, ans;

int a[100010], b[100010];
int P;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	
	cin >> P;
	
	cin >> n;
	
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	cin >> m;
	
	for (int j = 1; j <= m; j ++ ) cin >> b[j];
	
	reverse(a + 1, a + 1 + n);
	reverse(b + 1, b + 1 + m);
	
	LL p = 1;
	LL res = 0;
	
	for (int i = 1; i <= max(n, m); i ++ )
	{
		int t = max(max(a[i], b[i]) + 1, 2);
		
		res = (res + p * (a[i] - b[i]) % mod) % mod;
		p = p * t % mod;
		
	}
	
	cout << (res % mod + mod) % mod << endl;
	
    return 0;
}